Metrum Research Group @MetrumRG
Jonathan Sidi @yoniceedee
#devtools::install_github("metrumresearchgroup/ggedit",subdir="ggedit")
rm(list=ls())
library(ggedit)
#?ggedit
p0=list(
Scatter=iris%>%ggplot(aes(x =Sepal.Length,y=Sepal.Width))+
geom_point(aes(colour=Species),size=6),
ScatterFacet=iris%>%ggplot(aes(x =Sepal.Length,y=Sepal.Width))+
geom_point(aes(colour=Species),size=6)+
geom_line(linetype=2)+
facet_wrap(~Species,scales='free')+
labs(title='Some Title')
)
#a=ggedit(p.in = p0,verbose = T) #run ggedit
load('RstudioExampleObj.rda') #pre-run example
ldply(a,names)
## .id V1 V2
## 1 UpdatedPlots Scatter ScatterFacet
## 2 UpdatedLayers Scatter ScatterFacet
## 3 UpdatedLayersElements Scatter ScatterFacet
## 4 UpdatedLayerCalls Scatter ScatterFacet
## 5 updatedScales Scatter ScatterFacet
## 6 UpdatedScalesCalls Scatter ScatterFacet
## 7 UpdatedThemes Scatter ScatterFacet
## 8 UpdatedThemeCalls Scatter ScatterFacet
plot(a)
comparePlots=c(p0,a$UpdatedPlots)
names(comparePlots)[c(3:4)]=paste0(names(comparePlots)[c(3:4)],"Updated")
plot(as.ggedit(comparePlots))
comparePlots$ScatterFacetNewTheme=p0$ScatterFacet+a$UpdatedThemes$Scatter
plot(as.ggedit(comparePlots[c("ScatterFacet","ScatterFacetNewTheme")]),
plot.layout = list(list(rows=1,cols=1),list(rows=2,cols=1))
)
#Using Remove and Replace Function ##Overlay two layers of same geom
(comparePlots$ScatterMistake=p0$Scatter+a$UpdatedLayers$ScatterFacet[[1]])
(comparePlots$ScatterNoLayer=p0$Scatter%>%
rgg(oldGeom = 'point'))
(comparePlots$ScatterNewLayer=p0$Scatter%>%
rgg(oldGeom = 'point',
oldGeomIdx = 1,
newLayer = a$UpdatedLayers$ScatterFacet[[1]]))
(comparePlots$ScatterNewLayerTheme=p0$Scatter%>%
rgg(oldGeom = 'point',
newLayer = a$UpdatedLayers$ScatterFacet[[1]])+
a$UpdatedThemes$Scatter)
(l=p0$Scatter$layers[[1]])
## mapping: colour = Species
## geom_point: na.rm = FALSE
## stat_identity: na.rm = FALSE
## position_identity
(l1=cloneLayer(l))
## mapping: colour = Species
## geom_point: na.rm = FALSE
## stat_identity: na.rm = FALSE
## position_identity
all.equal(l,l1)
## [1] TRUE
(l1.txt=cloneLayer(l,verbose = T))
## [1] "geom_point(mapping=aes(colour=Species),na.rm=FALSE,size=6,data=NULL,position=\"identity\",stat=\"identity\",show.legend=NA,inherit.aes=TRUE)"
(l2=eval(parse(text=l1.txt)))
## mapping: colour = Species
## geom_point: na.rm = FALSE
## stat_identity: na.rm = FALSE
## position_identity
all.equal(l,l2)
## [1] TRUE
#Original geom_point layer
parse(text=cloneLayer(p0$ScatterFacet$layers[[1]],verbose = T))
## expression(geom_point(mapping = aes(colour = Species), na.rm = FALSE,
## size = 6, data = NULL, position = "identity", stat = "identity",
## show.legend = NA, inherit.aes = TRUE))
#new Layer
parse(text=a$UpdatedLayerCalls$ScatterFacet[[1]])
## expression(geom_point(mapping = aes(colour = Species), na.rm = FALSE,
## size = 3, shape = 22, fill = "#BD2020", alpha = 1, stroke = 0.5,
## data = NULL, position = "identity", stat = "identity", show.legend = NA,
## inherit.aes = TRUE))
pTheme=list()
(pTheme$Base=plot(a$UpdatedThemes$Scatter))
(pTheme$Select=plot(a$UpdatedThemes$Scatter,themePart = c('plot','legend'),fnt = 18))
(pTheme$Compare=plot(obj=a$UpdatedThemes$Scatter,obj2 = ggplot2:::theme_get()))